using HCore.IOC.Beans.Factory.Attribute.Attr;
using HCore.IOC.Context.Event.Attr;
using HCore.Tests.Runtime.DianDianDian.Event;
using HCore.Tests.Runtime.DianDianDian.Model;
using HCore.Tests.Runtime.DianDianDian.System;
using HCore.Unity.Behaviour;
using UnityEngine;

namespace HCore.Tests.Runtime.DianDianDian.UI
{
    public class GamePanel : HCoreMonoBehaviour
    {
        [Autowired] private ICountDownSystem mCountDownSystem;
        [Autowired] private IGameModel mGameModel;

        private void Start()
        {
            mGameModel.RegisterGoldChangedListener(ActionGameModelGoldChanged);
            mGameModel.RegisterLifeChangedListener(ActionGameModelLifeChanged);
            mGameModel.RegisterScoreChangedListener(ActionGameModelScoreChanged);

            ActionGameModelScoreChanged(mGameModel.Score, 0);
        }

        private void Update()
        {
            // 20 帧更新一次
            if (Time.frameCount % 20 != 0) return;
            mCountDownSystem.Update();
            Get("Text-倒计时").Text.text = $"{mCountDownSystem.GetCurrentRemainSeconds()}s";
        }

        protected override void OnDestroy()
        {
            mGameModel.UnRegisterGoldChangedListener(ActionGameModelGoldChanged);
            mGameModel.UnRegisterLifeChangedListener(ActionGameModelLifeChanged);
            mGameModel.UnRegisterScoreChangedListener(ActionGameModelScoreChanged);
            base.OnDestroy();
        }

        [EventListener]
        public void OnGamePassEvent(GamePassEvent @event)
        {
            Get().SetActive(false);
        }

        private void ActionGameModelScoreChanged(int newValue, int oldValue)
        {
            Get("Text-得分").Text.text = $"{newValue}";
        }

        private void ActionGameModelLifeChanged(int newValue, int oldValue)
        {
            Get("Text-生命").Text.text = $"{newValue}";
        }

        private void ActionGameModelGoldChanged(int newValue, int oldValue)
        {
            Get("Text-金币").Text.text = $"{newValue}";
        }
    }
}